﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using deepzoomAccess.DTDBService;

namespace deepzoomAccess
{
    /// <summary>
    /// Simple program to provide some maintence updates to the deep zoom database via the deep zoom service.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Usage();
                return;
            }

            switch (args[0])
            {
                case "ResetUnfinishedTalks":
                    ResetUnifinishedTalks();
                    break;

                case "ResetTalk":
                    if (args.Length != 2)
                    {
                        Usage();
                        return;
                    }
                    ResetTalk(Convert.ToInt32(args[1]));
                    break;

                default:
                    Usage();
                    break;
            }
        }

        /// <summary>
        /// Reset the rendering state for a particular talk ID.
        /// </summary>
        /// <param name="p"></param>
        private static void ResetTalk(int p)
        {
            using (DTDBClient c = new DTDBClient())
            {
                if (!c.ValidTalkID(p))
                {
                    Console.WriteLine("That talk ID is not known by the system");
                    return;
                }
                c.ResetTalkStateToUnrendered(p);
                Console.WriteLine("Talks reset");
                DumpTalkInfo(c, p);
            }
        }

        /// <summary>
        /// List info about a particular talk.
        /// </summary>
        /// <param name="c"></param>
        /// <param name="p"></param>
        private static void DumpTalkInfo(DTDBClient c, int p)
        {
            Console.WriteLine("  -> ID " + p);
        }

        /// <summary>
        /// Reset all the vairous talks that are in the rendering state.
        /// </summary>
        private static void ResetUnifinishedTalks()
        {
            using (DTDBClient c = new DTDBClient())
            {
                int[] talks = c.ResetRenderingTalks();
                if (talks != null && talks.Length != 0)
                {
                    Console.WriteLine("Reset the following talks");
                    foreach (var tid in talks)
                    {
                        DumpTalkInfo(c, tid);
                    }
                }
                else
                {
                    Console.WriteLine("No talk id's reset");
                }
            }
        }

        private static void Usage()
        {
            Console.WriteLine("Usage: deepzoomAccess ResetUnfinishedTalks - resets talks in the rendering state to be unrendered");
            Console.WriteLine("       deepzoomAccess ResetTalk <id> - resets talk <id> to be unrendered");
        }
    }

}
